<template>
  <div id="app">
    <ContactUs />
    <router-view />
  </div>
</template>

<script setup>
import ContactUs from '@/components/ContactUs';
import { watch } from 'vue';
import { useStore } from 'vuex';
import { getIndexPlatformInfo } from '@/api/home/home';
import { useRoute } from 'vue-router';
import { get } from 'lodash';
const store = useStore();
const route = useRoute();
const getInfo = async () => {
  const res = await getIndexPlatformInfo();
  localStorage.setItem('code', get(res, 'data.data.qrUrl', ''));
  const userAgent = navigator.userAgent || navigator.vendor || window.opera;
  let isMobile =
    /android|webos|iphone|ipad|ipod|blackberry|iemobile|opera mini/i.test(
      userAgent,
    );
  if (isMobile) {
    if (res.data.data.h5addr) {
      window.location.href = res.data.data.h5addr;
    }
  }
  document.title = get(res, 'data.data.title', '');
  setFavicon(get(res, 'data.data.favicon', ''));
  store.dispatch('user/changeIndexInfo', res.data.data);
  store.dispatch('user/changePlatformInfo', res.data.data);
};
watch(route, () => {
  getInfo();
});
const setFavicon = (newIconUrl) => {
  // 创建新的 link 标签
  var newLink = document.createElement('link');
  newLink.rel = 'icon';
  newLink.type = 'image/x-icon';
  newLink.href = newIconUrl;

  // 获取并移除原有的 favicon link（如果有多个，可能需要更精确的选择器）
  var oldLinks = document.querySelectorAll('link[rel="icon"]');
  for (var i = 0; i < oldLinks.length; i++) {
    oldLinks[i].parentNode.removeChild(oldLinks[i]);
  }

  // 将新创建的 link 插入到 head 中
  document.head.appendChild(newLink);
};
</script>

<style lang="scss" scoped>
@import './style/bass.css';
</style>
